//package com.dlc.felear.xiaoensale.activity;
//
//import android.content.Intent;
//import android.os.Handler;
//import android.os.Message;
//import android.text.TextUtils;
//import android.util.Log;
//import android.view.View;
//import android.widget.Button;
//import android.widget.TextView;
//
//import com.alibaba.fastjson.TypeReference;
//import com.dlc.felear.xiaoensale.R;
//import com.dlc.felear.xiaoensale.constant.Constant;
//import com.dlc.felear.xiaoensale.customView.ConfirmDialog;
//import com.dlc.felear.xiaoensale.customView.CookingMenuLvDialog;
//import com.dlc.felear.xiaoensale.customView.LoadingDialog;
//import com.dlc.felear.xiaoensale.customView.ProgressView;
//import com.dlc.felear.xiaoensale.customView.ShowTispMsgDialog;
//import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
//import com.dlc.felear.xiaoensale.dao.OperatorDao;
//import com.dlc.felear.xiaoensale.deviceControlSf.GuotiActionControl;
//import com.dlc.felear.xiaoensale.deviceControlSf.OperatorAutoHandle;
//import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
//import com.dlc.felear.xiaoensale.entity.CookingMenu;
//import com.dlc.felear.xiaoensale.entity.HotInfo;
//import com.dlc.felear.xiaoensale.entity.Operator;
//import com.dlc.felear.xiaoensale.entity.OpertorResult;
//import com.dlc.felear.xiaoensale.helper.JsonHelper;
//import com.dlc.felear.xiaoensale.utils.HandleSocketDataUtil;
//import com.dlc.felear.xiaoensale.utils.PlayVoiceUtil;
//import com.itdlc.android.library.base.BaseActivity;
//
//import org.simple.eventbus.EventBus;
//import org.simple.eventbus.Subscriber;
//import org.simple.eventbus.ThreadMode;
//
//import java.util.Collections;
//import java.util.List;
//
//import butterknife.BindView;
//import butterknife.OnClick;
//
///**
// * 开始炒菜页面
// */
//public class CookingActivity extends BaseActivity implements Handler.Callback, ConfirmDialog.ConfirmDialogCallback,
//        SingleInputDialog.SingleDialogCallback, CookingMenuLvDialog.OnclickItemCallback {
//    private static final String TAG = "CookingActivity";
//    private static final int WHAT_CANCEL_DIALOG = 343;
//    public static final String EVENT_MENU_INFO = "event_menu_info";
//    @BindView(R.id.cc_tv_step)
//    ProgressView progressView;
//    @BindView(R.id.cc_tv_name)
//    TextView tvName;
//    @BindView(R.id.tv_power)
//    TextView tvPower;
//    @BindView(R.id.tv_temp)
//    TextView tvTemp;
//    @BindView(R.id.tv_step_tips)
//    TextView tvStepTips;
//    @BindView(R.id.tv_error)
//    TextView tvError;
//    @BindView(R.id.cc_llll)
//    View llll;
//    @BindView(R.id.cc_tv_dccpV)
//    TextView tvDccp;
//    @BindView(R.id.tv_step_cost_time)
//    TextView tvCostTime;
//    @BindView(R.id.cc_btn_cc)
//    Button btnCc;
//    private TopbarHelper topbarHelper;
//    private CookingMenu cookingMenu;
//    private boolean btnCcFlag;
//    private long totalCookingTIme = 0;
//    private LoadingDialog loadingDialog;
//    private Handler handler;
//    private ConfirmDialog confirmDialog;
//    private ShowTispMsgDialog dialog;
//    private CookingMenuLvDialog cookingMenuLvDialog;
//    private HandleSocketDataUtil handleSocketDataUtil;
//    private OperatorDao operatorDao;
//    private SingleInputDialog singleInputDialog;
//    private OperatorAutoHandle operatorAutoHandle;
//
//    @Override
//    protected int getLayoutRes() {
//        EventBus.getDefault().register(this);
//        return R.layout.activity_cooking;
//    }
//
//    @Override
//    protected void initialView() {
//        try {
//            topbarHelper = new TopbarHelper(this);
//            topbarHelper.setcon(R.drawable.arrow_left);
//            topbarHelper.setNavActivity(CookingMenuActivity.class);
//            topbarHelper.setTitle("开始炒菜");
//            handler = new Handler(this);
//            dialog = new ShowTispMsgDialog(this);
//            dialog.setCancelable(true);
//            confirmDialog = new ConfirmDialog(this, this);
//            cookingMenuLvDialog = new CookingMenuLvDialog(this, this);
//            operatorDao = new OperatorDao(this);
//            operatorAutoHandle = OperatorAutoHandle.getInstance(null, null, "cookingActivity");
//            initEtView();
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void initEtView() {
//        singleInputDialog = new SingleInputDialog(this, this);
//    }
//
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        EventBus.getDefault().unregister(this);
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        try {
//            OperatorAutoHandle.getInstance(null, null, TAG).setCookingPage(true);
//            handleSocketDataUtil = HandleSocketDataUtil.getInstance(this);
//            handleSocketDataUtil.setcookingPageStatus(true);
//            if (handleSocketDataUtil != null) {
//                initSelectMenuStatus(handleSocketDataUtil.getCacheMenus().size());
//            }
//            getDataMenu();
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void initSelectMenuStatus(int szie) {
//        try {
//            Log.e(TAG, "缓存的菜谱大小:" + szie);
//            if (szie <= 0) {
//                llll.setBackgroundColor(0xffeeeeee);
//                tvDccp.setVisibility(View.INVISIBLE);
//                return;
//            }
//            tvDccp.setText(szie + "");
//            tvDccp.setVisibility(View.VISIBLE);
//            llll.setBackgroundColor(0xC6F1B3);
//        } catch (Exception e) {
//            showShortToast("数据异常");
//        }
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        try {
//            handleSocketDataUtil.setcookingPageStatus(false);
//            operatorAutoHandle.setCookingPage(false);
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void getDataMenu() {
//        try {
//            CookingMenu temp = JsonHelper.parserJson(getIntent().getStringExtra("cookingMenu"), CookingMenu.class);
//            if (temp != null) {
//                cookingMenu = temp;
//                String strList = getIntent().getStringExtra("operators");
//                List<Operator> tempData = null;
//                if (!TextUtils.equals(strList, "operators")) {
//                    tempData = JsonHelper.parserJson(strList, new TypeReference<List<Operator>>() {
//                    });
//                    cookingMenu.setOperators(tempData);
//                    initMenuInfo(cookingMenu);
//                } else {
//                    cookingMenu.setOperators(null);
//                    initMenuInfo(cookingMenu);
//                }
//            } else {
//                showShortToast("退出炒菜界面，没有找到菜谱");
//                finish();
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    @Override
//    protected void initData() {
//
//    }
//
//    private void initMenuInfo(CookingMenu cookingMenu) {
//        try {
//            Log.e(TAG, "name=" + cookingMenu.getName() + " size=" + cookingMenu.getSpecification());
//            List<Operator> tempData = cookingMenu.getOperators();
//            if (tempData == null || tempData.isEmpty()) {
//                tempData = OperatorAutoHandle.getInstance(null, null, TAG)
//                        .changgeOperator(operatorDao.queryForAllByMID(cookingMenu.getId()),
//                                Float.parseFloat(cookingMenu.getSpecification()), cookingMenu.getParam1(), cookingMenu.getCookingType());
//            }
//            initMenuInfo(cookingMenu, tempData);
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void initMenuInfo(CookingMenu cookingMenu, List<Operator> datas) {
//        try {
//            this.cookingMenu = cookingMenu;
//            if (cookingMenu != null) {
//                topbarHelper.setkey_Value("id", cookingMenu.getId());
//                Collections.sort(datas);
//                cookingMenu.setOperators(datas);
//                initCookingTime(datas);
//                progressView.setIndicateSteps(datas);
//                String ttt = cookingMenu.getCookingType() == 1 ? "炒" : "焖";
//                tvName.setText(cookingMenu.getName() + "(" + cookingMenu.getSpecification() + "斤 " + ttt + ")");
//            } else {
//                showShortToast("菜谱信息为空，退出炒菜界面");
//                finish();
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//
//    private void initCookingTime(List<Operator> datas) {
//        try {
//            int costTime = (int) Math.ceil(operatorAutoHandle.getCostTime(datas) / 1000);
//            tvCostTime.setText("预估时间:" + costTime + "S");
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    @OnClick({R.id.cc_btn_cc, R.id.cc_btn_xg, R.id.cc_btn_dc, R.id.cc_btn_fw,
//            R.id.cc_btn_stop, R.id.cc_btn_fw1, R.id.cc_btn_ds, R.id.cc_llll})
//    public void onClick(View view) {
//        try {
//            switch (view.getId()) {
//                case R.id.cc_btn_cc:
//                    confirmDialog.showDialog("开始炒菜前，请确认炒菜步骤序号是否升序", 0);
//                    break;
//                case R.id.cc_btn_xg:
//                    confirmDialog.showDialog("确定开始洗锅", 1);
//                    break;
//                case R.id.cc_btn_dc:
//                    confirmDialog.showDialog("确定开始倒菜", 2);
//                    break;
//                case R.id.cc_btn_fw:
//                    confirmDialog.showDialog("确定复位锅体", 3);
//                    break;
//                case R.id.cc_btn_stop:
//                    confirmDialog.showDialog("确定取消炒菜", 4);
//                    break;
//                case R.id.cc_btn_fw1:
//                    singleInputDialog.setHintMsg("全部打开10，全部关闭11");
//                    singleInputDialog.showDialog(R.id.cc_btn_fw1);
//                    break;
//                case R.id.cc_btn_ds:
//                    singleInputDialog.setHintMsg("打开 1 停止 0");
//                    singleInputDialog.showDialog(R.id.cc_btn_ds);
////                    confirmDialog.showDialog("确定倒水", 6);
//                    break;
//                case R.id.cc_llll:
//                    showSelectMenu();
//                    break;
//            }
//        } catch (Exception e) {
//            Log.e(TAG, e.getMessage());
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void showSelectMenu() {
//        try {
//            List<CookingMenu> list = handleSocketDataUtil.getCacheMenus();
//            if (list != null && !list.isEmpty()) {
//                cookingMenuLvDialog.showDialog(list);
//            } else {
//                showLongToast("没有待炒菜谱");
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void initBtnCc() {
//        try {
//            if (cookingMenu.getOperators() == null || cookingMenu.getOperators().isEmpty()) {
//                showShortToast("该菜谱没有炒菜步骤");
//                return;
//            }
//            if (TextUtils.isEmpty(cookingMenu.getParam1())) {
//                showShortToast("该菜谱没有设置计算公式参数");
//                return;
//            }
//            if (!btnCcFlag) {
//                operatorAutoHandle.setCurrentCookingMenu(cookingMenu);
//                totalCookingTIme = System.currentTimeMillis();
//                sendMenuInfo(cookingMenu.getOperators());
//                btnCcFlag = true;
//                handleSocketDataUtil.setCookingStatus(true);
//                btnCc.setText("正在炒菜");
//                btnCc.setBackgroundResource(R.drawable.btn_cc_l1_shape);
//                progressView.setIndicateSteps(cookingMenu.getOperators());
//                progressView.setCurrentIndex(cookingMenu.getOperators().size(), 0);
//            } else {
//                showLongToast("正在炒菜中...");
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//
//    private void sendMenuInfo(List<Operator> data) {
//        EventBus.getDefault().post(data, Constant.EVENT_BUS_MENU_INFO);
//    }
//
//    @Subscriber(tag = Constant.DEVICE_COOK_STEP)
//    public void OperatorAutoHandleresult(OpertorResult opertorResult) {
//        try {
//            Log.e(TAG, "totalStep=" + opertorResult.getTotalStep()
//                    + " current=" + opertorResult.getCurrentStep() + " 操作：" + opertorResult.getMsg());
//            if (opertorResult.getTotalStep() == opertorResult.getCurrentStep()) {
//                btnCc.setText("炒菜完成");
//                btnCcFlag = false;
//                btnCc.setBackgroundResource(R.drawable.btn_cc_l_shape);
//                PlayVoiceUtil.getInstance(this).playRfidVoice(10);
//                handleSocketDataUtil.removeMenuInfo(cookingMenu.getName() + "_" + cookingMenu.getSpecification());
//                Log.e(TAG, "炒菜总耗时：" + (System.currentTimeMillis() - totalCookingTIme) / 1000 + "秒");
//            }
//            progressView.setCurrentIndex(opertorResult.getTotalStep(), opertorResult.getCurrentStep());
//            tvStepTips.setText(progressView.getCurrentStep(opertorResult.getCurrentStep() - 1));
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//
//    @Subscriber(tag = EVENT_MENU_INFO)
//    public void recieveMenu(CookingMenu cookingMenu) {
//        try {
////            Log.e(TAG, "recieveMenu:" + JsonHelper.toJson(cookingMenu));
//            handleSocketDataUtil.setCookingStatus(true);
//            initMenuInfo(cookingMenu);
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    @Subscriber(tag = Constant.DEVICE_ERROR_MSG_SHOW)
//    public void warnErrorMsg(String msg) {
//        try {
//            if (TextUtils.isEmpty(msg)) {
//                dialog.closeDialog();
//                return;
//            }
//            dialog.setMsg(msg);
//            dialog.setDialog("设备故障");
//            allStop();
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private void sendCmd(Operator operator) {
//        showDialog();
//        EventBus.getDefault().post(operator, Constant.ACTION_CMD_INFO_DEBUG);
//    }
//
//    private void showDialog() {
//        try {
//            if (loadingDialog == null) {
//                loadingDialog = new LoadingDialog(this, "正在执行...");
//            }
//            handler.sendEmptyMessageDelayed(WHAT_CANCEL_DIALOG, 1000);
//            loadingDialog.show();
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//
//    @Override
//    public boolean handleMessage(Message msg) {
//        try {
//            if (msg.what == WHAT_CANCEL_DIALOG && loadingDialog != null) {
//                loadingDialog.close();
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//        return false;
//    }
//
//
//    @Override
//    public void selectResult(int value, int id) {
//        try {
//            Log.e(TAG, "value=" + value + " id=" + id);
//            if (value == 1) {
//                switch (id) {
//                    case 0:
//                        initBtnCc();
//                        break;
//                    case 1:
//                        if (!btnCcFlag) {
//                            handleSocketDataUtil.setCookingStatus(false);
//                            sendCmd(getOperator(Constant.ACTION_NAME_DS, 255, 255));
//                        } else {
//                            showShortToast("不能执行洗锅，炒菜未完成");
//                        }
//                        break;
//                    case 2:
//                        if (!btnCcFlag) {
//                            cgLuoji();
//                        } else {
//                            showShortToast("不能执行倒菜出锅，炒菜未完成");
//                        }
//                        break;
//                    case 3:
//                        handleSocketDataUtil.setCookingStatus(false);
//                        sendCmd(getOperator(Constant.ACTION_NAME_FW, 1, 1));
//                        break;
//                    case 4:
//                        sendAllStopCmd();
//                        break;
//                    case 5:
//                        sendCmd(getOperator(Constant.ACTION_NAME_FW, 0, 0));
//                        break;
//                    case 6:
//                        sendCmd(getOperator(Constant.ACTION_NAME_DS, 0, 0));
//                        break;
//                }
//            }
//        } catch (Exception e) {
//            Log.e(TAG, e.getMessage());
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    /**
//     * 出锅倒菜 逻辑
//     */
//    private void cgLuoji() {
//        try {
//            handleSocketDataUtil.setCookingStatus(false);
//            handleSocketDataUtil.removeMenuInfo(cookingMenu.getName() + "_" + cookingMenu.getSpecification());
//            sendCmd(getOperator(Constant.ACTION_NAME_DS, 255, 255));
//            if (handleSocketDataUtil.getCacheMenus() != null || handleSocketDataUtil.getCacheMenus().isEmpty()) {
//                startActivity(new Intent(this, CookingMenuActivity.class));
//                finish();
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    private Operator getOperator(String name, int value1, int value2) {
//        Operator operator = new Operator();
//        operator.setName(name);
//        operator.setValue(value1);
//        operator.setValue1(value2);
//        return operator;
//    }
//
//    private void sendAllStopCmd() {
//        try {
//            allStop();
//            EventBus.getDefault().post("cancel1", Constant.EVENT_BUS_MENU_CANCEL);
//            handleSocketDataUtil.removeMenuInfo(cookingMenu.getName() + "_" + cookingMenu.getSpecification());
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    @Override
//    public void SingleDialogInputResult(String value, int id) {
//        try {
//            Log.e(TAG, "value=" + value);
//            if (id == R.id.cc_btn_ds) {
//                if(TextUtils.equals(value,"0")){
//                    sendCmd(getOperator(Constant.ACTION_NAME_TZJB, 0, 0));
//                }else if(TextUtils.equals(value,"1")){
//                    sendCmd(getOperator(Constant.ACTION_NAME_KSJB, 2, 2));
//                }
//            }
//            if (id == R.id.cc_btn_fw) {
//                sendCmd(getOperator(Constant.ACTION_NAME_FW, Integer.parseInt(value), Integer.parseInt(value)));
//            }
//            if (TextUtils.equals(value, "10") || TextUtils.equals(value, "11")) {
//                sendCmd(getOperator(Constant.ACTION_NAME_JSC, Integer.parseInt(value), Integer.parseInt(value)));
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//
//    @Subscriber(mode = ThreadMode.MAIN, tag = Constant.ACTION_HOT_INFO_RESULT)
//    public void recieveHotInfo(HotInfo hotInfo) {
//        try {
//            if (hotInfo != null) {
//                tvError.setText(hotInfo.getMsg());
//                tvPower.setText(hotInfo.getPower() + "档");
//                tvTemp.setText(hotInfo.getTemp() + "℃");
//            }
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    @Subscriber(tag = Constant.SHOW_MENU_SIZE)
//    public void showMenuSizeValue(int size) {
//        initSelectMenuStatus(size);
//    }
//
//    private void allStop() {
//        try {
//            handleSocketDataUtil.setCookingStatus(false);
//            btnCcFlag = false;
//            btnCc.setText("因故障停止炒菜");
//            btnCc.setBackgroundResource(R.drawable.btn_cc_l2_shape);
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//
//    @Override
//    public void onclickItem(CookingMenu deviceInfo) {
//        try {
//            initMenuInfo(deviceInfo);
//        } catch (Exception e) {
//            showShortToast("数据异常:" + e.getMessage());
//            finish();
//        }
//    }
//}

package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.ConfirmDialog;
import com.dlc.felear.xiaoensale.customView.CookingMenuLvDialog;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.customView.ProgressView;
import com.dlc.felear.xiaoensale.customView.ShowTispMsgDialog;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.dao.MenuFoodMaterialDao;
import com.dlc.felear.xiaoensale.dao.OperatorDao;
import com.dlc.felear.xiaoensale.deviceControlSf.OperatorAutoHandle;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.HotInfo;
import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.entity.OpertorResult;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.SendData;
import com.dlc.felear.xiaoensale.utils.HandleSocketDataUtil;
import com.dlc.felear.xiaoensale.utils.PlayVoiceUtil;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 开始炒菜页面
 */
public class CookingActivity extends BaseActivity implements Handler.Callback, ConfirmDialog.ConfirmDialogCallback,
        SingleInputDialog.SingleDialogCallback, CookingMenuLvDialog.OnclickItemCallback {
    private static final String TAG = "CookingActivity";
    private static final int WHAT_CANCEL_DIALOG = 343;
    public static final String EVENT_MENU_INFO = "event_menu_info";
    @BindView(R.id.cc_tv_step)
    ProgressView progressView;
    @BindView(R.id.cc_tv_name)
    TextView tvName;
    @BindView(R.id.tv_power)
    TextView tvPower;
    @BindView(R.id.tv_temp)
    TextView tvTemp;
    @BindView(R.id.tv_step_tips)
    TextView tvStepTips;
    @BindView(R.id.tv_error)
    TextView tvError;
    @BindView(R.id.cc_llll)
    View llll;
    @BindView(R.id.cc_tv_dccpV)
    TextView tvDccp;
    @BindView(R.id.tv_step_cost_time)
    TextView tvCostTime;
    @BindView(R.id.cc_btn_cc)
    Button btnCc;
    private TopbarHelper topbarHelper;
    private CookingMenu cookingMenu;
    private boolean btnCcFlag;
    private long totalCookingTIme = 0;
    private LoadingDialog loadingDialog;
    private Handler handler;
    private ConfirmDialog confirmDialog;
    private ShowTispMsgDialog dialog;
    private CookingMenuLvDialog cookingMenuLvDialog;
    private HandleSocketDataUtil handleSocketDataUtil;
    private OperatorDao operatorDao;
    private SingleInputDialog singleInputDialog;
    private OperatorAutoHandle operatorAutoHandle;
    private MenuFoodMaterialDao menuFoodMaterialDao;

    @Override
    protected int getLayoutRes() {
        EventBus.getDefault().register(this);
        return R.layout.activity_cooking;
    }

    @Override
    protected void initialView() {
        try {
            topbarHelper = new TopbarHelper(this);
            topbarHelper.setcon(R.drawable.arrow_left);
            topbarHelper.setNavActivity(CookingMenuActivity.class);
            topbarHelper.setTitle("开始炒菜");
            handler = new Handler(this);
            dialog = new ShowTispMsgDialog(this);
            dialog.setCancelable(true);
            confirmDialog = new ConfirmDialog(this, this);
            cookingMenuLvDialog = new CookingMenuLvDialog(this, this);
            operatorDao = new OperatorDao(this);
            operatorAutoHandle = OperatorAutoHandle.getInstance(null, null, "cookingActivity");
            menuFoodMaterialDao=new MenuFoodMaterialDao(this);
            initEtView();
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void initEtView() {
        singleInputDialog = new SingleInputDialog(this, this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            OperatorAutoHandle.getInstance(null, null, TAG).setCookingPage(true);
            handleSocketDataUtil = HandleSocketDataUtil.getInstance(this);
            handleSocketDataUtil.setcookingPageStatus(true);
            if (handleSocketDataUtil != null) {
                initSelectMenuStatus(handleSocketDataUtil.getCacheMenus().size());
            }
            getDataMenu();
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void initSelectMenuStatus(int szie) {
        try {
            Log.e(TAG, "缓存的菜谱大小:" + szie);
            if (szie <= 0) {
                llll.setBackgroundColor(0xffeeeeee);
                tvDccp.setVisibility(View.INVISIBLE);
                return;
            }
            tvDccp.setText(szie + "");
            tvDccp.setVisibility(View.VISIBLE);
            llll.setBackgroundColor(0xC6F1B3);
        } catch (Exception e) {
            showShortToast("数据异常");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            handleSocketDataUtil.setcookingPageStatus(false);
            operatorAutoHandle.setCookingPage(false);
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void getDataMenu() {
        try {
            CookingMenu temp = JsonHelper.parserJson(getIntent().getStringExtra("cookingMenu"), CookingMenu.class);
            if (temp != null) {
                temp.setFoodMaterials(menuFoodMaterialDao.queryForAllByCId(temp.getId()));
                cookingMenu = temp;
                String strList = getIntent().getStringExtra("operators");
                List<Operator> tempData = null;
                if (!TextUtils.equals(strList, "operators")) {
                    tempData = JsonHelper.parserJson(strList, new TypeReference<List<Operator>>() {
                    });
                    cookingMenu.setOperators(tempData);
                    initMenuInfo(cookingMenu);
                } else {
                    cookingMenu.setOperators(null);
                    initMenuInfo(cookingMenu);
                }
            } else {
                showShortToast("退出炒菜界面，没有找到菜谱");
                finish();
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    @Override
    protected void initData() {

    }

    private void initMenuInfo(CookingMenu cookingMenu) {
        try {
            Log.e(TAG, "name=" + cookingMenu.getName() + " size=" + cookingMenu.getSpecification());
            List<Operator> tempData = cookingMenu.getOperators();
            if (tempData == null || tempData.isEmpty()) {
                tempData = OperatorAutoHandle.getInstance(null, null, TAG)
                        .changgeOperator(operatorDao.queryForAllByMID(cookingMenu.getId()),
                                Float.parseFloat(cookingMenu.getSpecification()), cookingMenu.getParam1(), cookingMenu.getCookingType());
            }
            initMenuInfo(cookingMenu, tempData);
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void initMenuInfo(CookingMenu cookingMenu, List<Operator> datas) {
        try {
            this.cookingMenu = cookingMenu;
            if (cookingMenu != null) {
                topbarHelper.setkey_Value("id", cookingMenu.getId());
                Collections.sort(datas);
                cookingMenu.setOperators(datas);
                initCookingTime(datas);
                progressView.setIndicateSteps(datas);
                String ttt = cookingMenu.getCookingType() == 1 ? "炒" : "焖";
                tvName.setText(cookingMenu.getName() + "(" + cookingMenu.getSpecification() + "斤 " + ttt + ")");
            } else {
                showShortToast("菜谱信息为空，退出炒菜界面");
                finish();
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }


    private void initCookingTime(List<Operator> datas) {
        try {
            int costTime = (int) Math.ceil(operatorAutoHandle.getCostTime(datas) / 1000);
            if(cookingMenu!=null){
                cookingMenu.setDuration(costTime);
            }
            tvCostTime.setText("预估时间:" + costTime + "S");
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    @OnClick({R.id.cc_btn_cc, R.id.cc_btn_xg, R.id.cc_btn_dc, R.id.cc_btn_fw,
            R.id.cc_btn_stop, R.id.cc_btn_fw1, R.id.cc_btn_ds, R.id.cc_llll})
    public void onClick(View view) {
        try {
            switch (view.getId()) {
                case R.id.cc_btn_cc:
                    confirmDialog.showDialog("开始炒菜前，请确认炒菜步骤序号是否升序", 0);
                    break;
                case R.id.cc_btn_xg:
                    confirmDialog.showDialog("确定开始洗锅", 1);
                    break;
                case R.id.cc_btn_dc:
                    confirmDialog.showDialog("确定开始倒菜", 2);
                    break;
                case R.id.cc_btn_fw:
                    confirmDialog.showDialog("确定复位锅体", 3);
                    break;
                case R.id.cc_btn_stop:
                    confirmDialog.showDialog("确定取消炒菜", 4);
                    break;
                case R.id.cc_btn_fw1:
                    singleInputDialog.setHintMsg("全部打开10，全部关闭11");
                    singleInputDialog.showDialog(R.id.cc_btn_fw1);
                    break;
                case R.id.cc_btn_ds:
                    startActivity(new Intent(this,ManualDebugActivity.class));
//                    confirmDialog.showDialog("确定倒水", 6);
                    break;
                case R.id.cc_llll:
                    showSelectMenu();
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void showSelectMenu() {
        try {
            List<CookingMenu> list = handleSocketDataUtil.getCacheMenus();
            if (list != null && !list.isEmpty()) {
                cookingMenuLvDialog.showDialog(list);
            } else {
                showLongToast("没有待炒菜谱");
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void initBtnCc() {
        try {
//            SendData.sendRecordMenuInfo(cookingMenu);
            if (cookingMenu.getOperators() == null || cookingMenu.getOperators().isEmpty()) {
                showShortToast("该菜谱没有炒菜步骤");
                return;
            }
            if (TextUtils.isEmpty(cookingMenu.getParam1())) {
                showShortToast("该菜谱没有设置计算公式参数");
                return;
            }
            if (!btnCcFlag) {
                operatorAutoHandle.setCurrentCookingMenu(cookingMenu);
                totalCookingTIme = System.currentTimeMillis();
                sendMenuInfo(cookingMenu.getOperators());
                btnCcFlag = true;
                handleSocketDataUtil.setCookingStatus(true);
                btnCc.setText("正在炒菜");
                btnCc.setBackgroundResource(R.drawable.btn_cc_l1_shape);
                progressView.setIndicateSteps(cookingMenu.getOperators());
                progressView.setCurrentIndex(cookingMenu.getOperators().size(), 0);
            } else {
                showLongToast("正在炒菜中...");
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }


    private void sendMenuInfo(List<Operator> data) {
        EventBus.getDefault().post(data, Constant.EVENT_BUS_MENU_INFO);
    }

    @Subscriber(tag = Constant.DEVICE_COOK_STEP)
    public void OperatorAutoHandleresult(OpertorResult opertorResult) {
        try {
            Log.e(TAG, "totalStep=" + opertorResult.getTotalStep()
                    + " current=" + opertorResult.getCurrentStep() + " 操作：" + opertorResult.getMsg());
            if (opertorResult.getTotalStep() == opertorResult.getCurrentStep()) {
                btnCc.setText("炒菜完成");
                btnCcFlag = false;
                btnCc.setBackgroundResource(R.drawable.btn_cc_l_shape);
                PlayVoiceUtil.getInstance(this).playRfidVoice(10);
                handleSocketDataUtil.removeMenuInfo(cookingMenu.getName() + "_" + cookingMenu.getSpecification());
                Log.e(TAG, "炒菜总耗时：" + (System.currentTimeMillis() - totalCookingTIme) / 1000 + "秒");
                SendData.sendRecordMenuInfo(cookingMenu);
            }
            progressView.setCurrentIndex(opertorResult.getTotalStep(), opertorResult.getCurrentStep());
            tvStepTips.setText(progressView.getCurrentStep(opertorResult.getCurrentStep() - 1));
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }


    @Subscriber(tag = EVENT_MENU_INFO)
    public void recieveMenu(CookingMenu cookingMenu) {
        try {
//            Log.e(TAG, "recieveMenu:" + JsonHelper.toJson(cookingMenu));
            handleSocketDataUtil.setCookingStatus(true);
            initMenuInfo(cookingMenu);
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    @Subscriber(tag = Constant.DEVICE_ERROR_MSG_SHOW)
    public void warnErrorMsg(String msg) {
        try {
            if (TextUtils.isEmpty(msg)) {
                dialog.closeDialog();
                return;
            }
            dialog.setMsg(msg);
            dialog.setDialog("设备故障");
            allStop();
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void sendCmd(Operator operator) {
        showDialog();
        EventBus.getDefault().post(operator, Constant.ACTION_CMD_INFO_DEBUG);
    }

    private void showDialog() {
        try {
            if (loadingDialog == null) {
                loadingDialog = new LoadingDialog(this, "正在执行...");
            }
            handler.sendEmptyMessageDelayed(WHAT_CANCEL_DIALOG, 1000);
            loadingDialog.show();
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }


    @Override
    public boolean handleMessage(Message msg) {
        try {
            if (msg.what == WHAT_CANCEL_DIALOG && loadingDialog != null) {
                loadingDialog.close();
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
        return false;
    }


    @Override
    public void selectResult(int value, int id) {
        try {
            Log.e(TAG, "value=" + value + " id=" + id);
            if (value == 1) {
                switch (id) {
                    case 0:
                        initBtnCc();
                        break;
                    case 1:
                        if (!btnCcFlag) {
                            handleSocketDataUtil.setCookingStatus(false);
                            sendCmd(getOperator(Constant.ACTION_NAME_XG, 255, 255));
                        } else {
                            showShortToast("不能执行洗锅，炒菜未完成");
                        }
                        break;
                    case 2:
                        if (!btnCcFlag) {
                            cgLuoji();
                        } else {
                            showShortToast("不能执行倒菜出锅，炒菜未完成");
                        }
                        break;
                    case 3:
                        handleSocketDataUtil.setCookingStatus(false);
                        sendCmd(getOperator(Constant.ACTION_NAME_FW, 1, 1));
                        break;
                    case 4:
                        sendAllStopCmd();
                        break;
                    case 5:
                        sendCmd(getOperator(Constant.ACTION_NAME_FW, 0, 0));
                        break;
                    case 6:
                        sendCmd(getOperator(Constant.ACTION_NAME_DS, 0, 0));
                        break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    /**
     * 出锅倒菜 逻辑
     */
    private void cgLuoji() {
        try {
            handleSocketDataUtil.setCookingStatus(false);
            handleSocketDataUtil.removeMenuInfo(cookingMenu.getName() + "_" + cookingMenu.getSpecification());
            sendCmd(getOperator(Constant.ACTION_NAME_DC, 255, 255));
            if (handleSocketDataUtil.getCacheMenus() != null || handleSocketDataUtil.getCacheMenus().isEmpty()) {
                startActivity(new Intent(this, CookingMenuActivity.class));
                finish();
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private Operator getOperator(String name, int value1, int value2) {
        Operator operator = new Operator();
        operator.setName(name);
        operator.setValue(value1);
        operator.setValue1(value2);
        return operator;
    }

    private void sendAllStopCmd() {
        try {
            allStop();
            EventBus.getDefault().post("cancel1", Constant.EVENT_BUS_MENU_CANCEL);
            handleSocketDataUtil.removeMenuInfo(cookingMenu.getName() + "_" + cookingMenu.getSpecification());
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    @Override
    public void SingleDialogInputResult(String value, int id) {
        try {
            Log.e(TAG, "value=" + value);
            if (id == R.id.cc_btn_fw) {
                sendCmd(getOperator(Constant.ACTION_NAME_FW, Integer.parseInt(value), Integer.parseInt(value)));
            }
            if (TextUtils.equals(value, "10") || TextUtils.equals(value, "11")) {
                sendCmd(getOperator(Constant.ACTION_NAME_JSC, Integer.parseInt(value), Integer.parseInt(value)));
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }


    @Subscriber(mode = ThreadMode.MAIN, tag = Constant.ACTION_HOT_INFO_RESULT)
    public void recieveHotInfo(HotInfo hotInfo) {
        try {
            if (hotInfo != null) {
                tvError.setText(hotInfo.getMsg());
                tvPower.setText(hotInfo.getPower() + "档");
                tvTemp.setText(hotInfo.getTemp() + "℃");
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    @Subscriber(tag = Constant.SHOW_MENU_SIZE)
    public void showMenuSizeValue(int size) {
        initSelectMenuStatus(size);
    }

    private void allStop() {
        try {
            handleSocketDataUtil.setCookingStatus(false);
            btnCcFlag = false;
            btnCc.setText("因故障停止炒菜");
            btnCc.setBackgroundResource(R.drawable.btn_cc_l2_shape);
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    @Override
    public void onclickItem(CookingMenu deviceInfo) {
        try {
            initMenuInfo(deviceInfo);
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }
}